package nix.mall.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nix.mall.common.pojo.EasyUIDataGridResult;
import nix.mall.common.pojo.NixMallResult;
import nix.mall.content.service.ContentService;
import nix.mall.jedis.JedisClient;
import nix.mall.mapper.ContentMapper;
import nix.mall.pojo.Content;
import nix.mall.pojo.ContentExample;
import nix.mall.pojo.ContentExample.Criteria;
import nix.mall.utils.JsonUtils;

/**
 * 内容管理Service
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private ContentMapper contentMapper;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${INDEX_CONTENT}")
	private String INDEX_CONTENT;

	@Override
	public NixMallResult addContent(Content content) {
		// 不全属性
		content.setCreated(new Date());
		content.setUpdated(new Date());
		// 插入数据库
		contentMapper.insert(content);
		// 同步缓存：删除对应缓存
		jedisClient.hdel(INDEX_CONTENT, content.getCategoryId().toString());
		return NixMallResult.ok();
	}
	
	@Override
	public EasyUIDataGridResult getContentListByCatId(Long categoryId, Integer page, Integer rows) {
		// 分页信息
		PageHelper.startPage(page, rows);
		// 执行分页
		List<Content> list = getContentListByCatId(categoryId);
		// 取查询结果
		PageInfo<Content> pageInfo = new PageInfo<>(list);
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		result.setTotal(pageInfo.getTotal());
		// 返回结果		
		return result;
	}

	@Override
	public Content getContentById(Long id) {
		Content content = contentMapper.selectByPrimaryKey(id);
		return content;
	}

	@Override
	public NixMallResult editContent(Content content) {
		content.setUpdated(new Date());
		contentMapper.updateByPrimaryKeySelective(content);
		// 同步缓存：删除对应缓存
		jedisClient.hdel(INDEX_CONTENT, content.getCategoryId().toString());
		return NixMallResult.ok();
	}

	@Override
	public NixMallResult delContentByIds(String ids) {
		// ids转List
		String[] strings = ids.split(",");
		List<Long> idList = new ArrayList<>();
		for (String string : strings) {
			idList.add(Long.parseLong(string));
		}
		// 查询条件
		ContentExample example = new ContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(idList);
		// 同步缓存：删除对应缓存
		List<Content> list = contentMapper.selectByExample(example);
		if (list.size() >= 1) {
			jedisClient.hdel(INDEX_CONTENT, list.get(0).getCategoryId().toString());
		}
		// 删除数据
		contentMapper.deleteByExample(example);
		return NixMallResult.ok();
	}

	@Override
	public List<Content> getContentListByCatId(Long categoryId) {
		// 先查询缓存
		// 添加缓存不影响正常逻辑
		try {
			// 查询缓存
			String json = jedisClient.hget(INDEX_CONTENT, categoryId + "");
			if (StringUtils.isNoneBlank(json)) {
				List<Content> list = JsonUtils.jsonToList(json, Content.class);
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 缓存没有命中，查询数据库
		// 设置条件
		ContentExample example = new ContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		// 执行查询
		List<Content> list = contentMapper.selectByExample(example);
		// 把结果添加至缓存
		try {
			jedisClient.hset(INDEX_CONTENT, categoryId + "", JsonUtils.objectToJson(list));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
